using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// JG_JobsAssessTemplate
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class JG_JobsAssessTemplateController : CoreApiController
    {
        private readonly JG_JobsAssessTemplateService _service;
        private readonly JG_JobsAssessTemplateDetailService _serviceD;
        /// <summary>
        /// JG_JobsAssessTemplate
        ///</summary>
        public JG_JobsAssessTemplateController(JG_JobsAssessTemplateService service, JG_JobsAssessTemplateDetailService serviceD)
        {
            _service = service;
            _serviceD = serviceD;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(JG_JobsAssessTemplateController) + "_" + nameof(JG_JobsAssessTemplateController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<JG_JobsAssessTemplateDto>
                pagingQueryModel)
        {
            IPageModel<JG_JobsAssessTemplateDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(JG_JobsAssessTemplateController) + "_" + nameof(JG_JobsAssessTemplateController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(JG_JobsAssessTemplateDto dto)
        {
            dto.CreateFactory = UserIdentity.Factory;
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                var list = dto.JG_JobsAssessTemplateDetail.ToList();

                dto.JG_JobsAssessTemplateDetail.Clear();
                await _service.UpdateAsync(dto);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].JobsAssessTemplateID = dto.Id;
                    if (list[i].CreateTime == null)
                    {
                        await _serviceD.InsertAsync(list[i]);
                    }
                    else
                    {
                        await _serviceD.UpdateAsync(list[i]);
                    }
                }
                //await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet("DeleteMuti")]
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(JG_JobsAssessTemplateController) + "_" + nameof(JG_JobsAssessTemplateController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        [HttpPost("Get")]
        public async Task<IActionResult>
            Get(PagingModel<JG_JobsAssessTemplateDto>
                pagingQueryModel)
        {
            //PagingModel<JG_JobsAssessTemplateDto>
            //    pagingQueryModel = new PagingModel<JG_JobsAssessTemplateDto>();
            //pagingQueryModel.Model.postID = id;
            //pagingQueryModel.Offset = 0;
            //pagingQueryModel.Limit = 1;

            IPageModel<JG_JobsAssessTemplateDto> result = await _service.GetList(pagingQueryModel);
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
            //if (result.RowCount > 0)
            //{
            //    var model = result.PageData[0];
            //    if (model == null) return this.JsonFaild("未找到对应数据");
            //    return this.JsonSuccess(model);
            //}
            //else
            //{
            //    return this.JsonSuccess(null);

            //}
            //var model = await _service.GetAsync(id);
            //if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            //return this.JsonSuccess(model);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(JG_JobsAssessTemplateController) + "_" + nameof(JG_JobsAssessTemplateController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<JG_JobsAssessTemplateDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion


    }
}
